package com.datang.litejvm.print;


import com.datang.litejvm.constant.*;

/**
 * @author: 顶风少年
 * @Description: 输出常量池
 * @date: 20:39 2022/6/13
 **/
public class ConstantPoolPrinter {
    ConstantPool pool;

    ConstantPoolPrinter(ConstantPool pool) {
        this.pool = pool;
    }

    public void print() {
        System.out.println("Constant Pool:");
        PrintTextVisitor printTextVisitor = new PrintTextVisitor(pool);
        for (int i = 1; i <= pool.getSize(); i++) {
            if (i < 10) {
                System.out.print(" #" + i + " = ");
            } else {
                System.out.print("#" + i + " = ");
            }

            ConstantInfo constantInfo = pool.getConstantInfo(i);
            constantInfo.accept(printTextVisitor);
        }
    }
}
